import requests


def get_price(start, end, time):
    url = f"http://api.airplane.com/check?start={start}&end={end}&time={time}"
    response = requests.get(url)
    data = response.json()
    if data["available"] == 1:
        return data["price"]
    else:
        return float("inf")


def get_best_price(start, end):
    best_price = float("inf")
    for time in range(1, 4):
        price = get_price(start, end, time)
        if price < best_price:
            best_price = price
    return best_price


def get_best_transfer_price(start, end):
    cities = ["BJ", "TJ", "SH", "NJ", "XA", "WH", "ZZ", "CS", "XM", "GZ", "SZ"]
    best_price = float("inf")
    for time in range(1, 4):
        if time == 1:
            transfer_times = 2
        elif time == 2:
            transfer_times = 1
        else:
            transfer_times = 0
        for i in range(transfer_times + 1):
            for city1 in cities:
                if city1 != start and city1 != end:
                    if i == 0:
                        price = get_price(start, end, time)
                    elif i == 1:
                        price1 = get_price(start, city1, time)
                        price2 = get_price(city1, end, time + 1)
                        price = price1 + price2
                    else:
                        for city2 in cities:
                            if city2 != start and city2 != end and city2 != city1:
                                price1 = get_price(start, city1, time)
                                price2 = get_price(city1, city2, time + 1)
                                price3 = get_price(city2, end, time + 2)
                                price = price1 + price2 + price3
                    if price < best_price:
                        best_price = price
    return best_price


best_direct_price = get_best_price("SZ", "BJ")
best_transfer_price = get_best_transfer_price("SZ", "BJ")
best_price = min(best_direct_price, best_transfer_price)

print(f"The best price from SZ to BJ is: {best_price}")
